package com.yuou.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuou.common.base.CoreServiceImpl;
import com.yuou.common.utils.BuildTree;
import com.yuou.common.utils.Tree;
import com.yuou.mapper.MenuMapper;
import com.yuou.mapper.RoleMenuMapper;
import com.yuou.model.Menu;
import java.util.Map;
import com.yuou.service.MenuService;
@Service
public class MenuServiceImpl extends CoreServiceImpl<MenuMapper, Menu> implements  MenuService {
	@Autowired
	private MenuMapper menuMapper;
	@Autowired
	private RoleMenuMapper roleMenuMapper;
	@Override
	public Set<String> listPerms(Integer userId) {
		List<String> perms = menuMapper.listUserPerms(userId);
        Set<String> permsSet = new HashSet<>();
        for (String perm : perms) {
            if (StringUtils.isNotBlank(perm)) {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
	}
	@Override
	public List<Menu> selectAll(Map<String, Object> map) {
		return menuMapper.selectAll(map);
	}
	@Override
	public int deleteByPrimaryKey(Integer id) {
		return menuMapper.deleteByPrimaryKey(id);
	}
	@Override
	public int selectCountByid(Integer id) {
		return menuMapper.selectCountByid(id);
	}
	@Override
	public int insertMenu(Menu menu) {
		return menuMapper.insertMenu(menu);
	}
	@Override
	public int updateByPrimaryKey(Menu menu) {
		return menuMapper.updateByPrimaryKey(menu);
	}
	@Override
	public Menu selectById(Integer id) {
		return menuMapper.selectById(id);
	}
	@Override
	public String selectNameByid(Integer parentId) {
		return menuMapper.selectNameByid(parentId);
	}
	@Override
	public Tree<Menu> getTree() {
		List<Tree<Menu>> trees = new ArrayList<Tree<Menu>>();
        List<Menu> Menus = menuMapper.selectAll(null);
        for (Menu sysMenu : Menus) {
            Tree<Menu> tree = new Tree<Menu>();
            tree.setId(String.valueOf(sysMenu.getId()));
            tree.setParentId(String.valueOf(sysMenu.getParentId()));
            tree.setText(sysMenu.getName());
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<Menu> t = BuildTree.build(trees);
        return t;
	}
	@Override
	public Tree<Menu> getTree(Integer id) {
		// 根据roleId查询权限
        List<Menu> menus = menuMapper.selectAll(null);
        List<Integer> menuIds = roleMenuMapper.listMenuIdByRoleId(id);
        //System.out.println(menuIds.toString());
        List<Integer> temp = menuIds;
        for (Menu menu : menus) {
            if (temp.contains(menu.getParentId())) {
            	for(int i=0;i<menuIds.size();i++){
            	    if(menuIds.get(i)==(menu.getParentId()))
            	    	menuIds.remove(i);
            	}
            }
        }
        List<Tree<Menu>> trees = new ArrayList<Tree<Menu>>();
        List<Menu> Menus = menuMapper.selectAll(null);
        for (Menu sysMenu : Menus) {
            Tree<Menu> tree = new Tree<Menu>();
            tree.setId(String.valueOf(sysMenu.getId()));
            tree.setParentId(String.valueOf(sysMenu.getParentId()));
            tree.setText(sysMenu.getName());
            Map<String, Object> state = new HashMap<>(16);
            Integer menuId = sysMenu.getId();
            //System.out.println("id========"+menuId);
            if (menuIds.contains(menuId)) {
                state.put("selected", true);
            } else {
                state.put("selected", false);
            }
            tree.setState(state);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<Menu> t = BuildTree.build(trees);
        return t;
	}
	@Override
	public Tree<Menu> getSysMenuTree(Integer id) {
		List<Tree<Menu>> trees = new ArrayList<Tree<Menu>>();
        List<Menu> Menus = baseMapper.listMenuByUserId(id);
        for (Menu sysMenu : Menus) {
            Tree<Menu> tree = new Tree<Menu>();
            tree.setId(String.valueOf(sysMenu.getId()));
            tree.setParentId(String.valueOf(sysMenu.getParentId()));
            tree.setText(sysMenu.getName());
            Map<String, Object> attributes = new HashMap<>(16);
            attributes.put("url", sysMenu.getUrl());
            attributes.put("icon", sysMenu.getIcon());
            tree.setAttributes(attributes);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<Menu> t = BuildTree.build(trees);
        return t;
	}
	@Override
	public List<Tree<Menu>> listMenuTree(Integer id) {
		List<Tree<Menu>> trees = new ArrayList<Tree<Menu>>();
        List<Menu> Menus = baseMapper.listMenuByUserId(id);
        for (Menu sysMenu : Menus) {
            Tree<Menu> tree = new Tree<Menu>();
            tree.setId(String.valueOf(sysMenu.getId()));
            tree.setParentId(String.valueOf(sysMenu.getParentId()));
            tree.setText(sysMenu.getName());
            Map<String, Object> attributes = new HashMap<>(16);
            attributes.put("url", sysMenu.getUrl());
            attributes.put("icon", sysMenu.getIcon());
            tree.setAttributes(attributes);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        List<Tree<Menu>> list = BuildTree.buildList(trees, "0");
        return list;
	}

}
